//
//  FMDBModel.m
//  TeamProject_Travel
//
//  Created by lanou3g on 16/4/5.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "FMDBModel.h"
#import "FMDatabase.h"

@implementation FMDBModel

{
    FMDatabase *fmdb;
}
+ (FMDBModel *)defaultFMDBModel
{
    static FMDBModel *fmdb1 = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        fmdb1 = [[FMDBModel alloc] init];
    });
    return fmdb1;
}

-(id)init
{
    if(self=[super init]){
        NSString *documenPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
        //数据库存储路径
        NSString *dbpath=[documenPath stringByAppendingPathComponent:@"AccountModel.db"];
//        NSLog(@"%@",dbpath);
        fmdb = [FMDatabase databaseWithPath:dbpath];
        //尝试打开数据库
        if([fmdb open]){
            [self createTable];
        }
    }
    return self;
}

//数据库建表
-(void)createTable
{
    NSArray *allArray=[NSArray arrayWithObjects:
                       @"CREATE TABLE IF NOT EXISTS AccountModel (t_id integer primary key autoincrement not null unique,category Varchar DEFAULT NULL,rests Varchar DEFAULT NULL,address Varchar DEFAULT NULL,time Varchar DEFAULT NULL,type Varchar DEFAULT NULL,total Varchar DEFAULT NULL,consumption Varchar DEFAULT NULL)",nil];
    for(NSString *sql in allArray){
        //建表，增、删、改都用这个方法
        if([fmdb executeUpdate:sql]){
            NSLog(@"创建成功");
        }else{
            NSLog(@"建表失败");
        }
    }
}

// 插入
- (void)insertAccount:(AccountModel *)accountModel
{
    NSString *sql=[NSString stringWithFormat:@"insert into AccountModel (t_id,category,rests,address,time,type,total,consumption) values (?,?,?,?,?,?,?,?)"];
    if (sql) {
        [fmdb executeUpdate:sql,accountModel.t_id,accountModel.category,accountModel.rests,accountModel.address,accountModel.time,accountModel.type,accountModel.total,accountModel.consumption];
    }
}


// 拿到所有数据
- (NSMutableArray *)getAllAccount
{
    NSMutableArray *arr = [NSMutableArray array];
    FMResultSet *rs=[fmdb executeQuery:@"SELECT * FROM AccountModel"];
    while ([rs next]){
        AccountModel *account=[[AccountModel alloc]init];
        account.t_id = [[rs stringForColumn:@"t_id"] integerValue];
        account.category = [rs stringForColumn:@"category"];
        account.rests = [rs stringForColumn:@"rests"];
        account.address = [rs stringForColumn:@"address"];
        account.time = [rs stringForColumn:@"time"];
        account.type = [rs stringForColumn:@"type"];
        account.total = [rs stringForColumn:@"total"];
        account.consumption = [rs stringForColumn:@"consumption"];
        
        [arr addObject:account];
    }
    return arr;
}

// 根据唯一标示获取对象
-(AccountModel *)getOneAccountWithName:(NSInteger)accountName
{
    FMResultSet *rs=[fmdb executeQuery:@"SELECT * FROM AccountModel where t_id=?",accountName];
    AccountModel *account=[[AccountModel alloc]init];
    while ([rs next]){
        account.t_id = [[rs stringForColumn:@"t_id"] integerValue];
        account.category = [rs stringForColumn:@"category"];
        account.rests = [rs stringForColumn:@"rests"];
        account.address = [rs stringForColumn:@"address"];
        account.time = [rs stringForColumn:@"time"];
        account.type = [rs stringForColumn:@"type"];
        account.total = [rs stringForColumn:@"total"];
        account.consumption = [rs stringForColumn:@"consumption"];
    }
    return account;
}

// 根据标识删除对象
-(void)deleteFriendWithAccountName:(NSString *)accountName
{
    BOOL a=[fmdb executeUpdate:@"DELETE FROM Collect WHERE t_id= ?",accountName];
    if (a) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
}

// 删除所有对象
- (void)deleteAllAccount
{
    BOOL a=[fmdb executeUpdate:@"DELETE FROM AccountModel"];
    if (a) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
}

- (void)UpdataAccountCategory:(NSString *)oldCategory withNewCategory:(NSString *)newCategory UpdataAccountRests:(NSString *)oldRests withNewRests:(NSString *)newRests UpdataAccountAddress:(NSString *)oldAddress withNewAddress:(NSString *)newAddress UpdataAccountTime:(NSString *)oldTime withNewTime:(NSString *)newTime UpdataAccountType:(NSString *)oldType withNewType:(NSString *)newType UpdataAccountTotal:(NSString *)oldTotal withNewTotal:(NSString *)newTotal UpdataAccountConsumption:(NSString *)oldConsumption withNewConsumption:(NSString *)newConsumption
{
    [fmdb executeUpdate:@"update AccountModel set category = ? where category = ?",newCategory,oldCategory];
    [fmdb executeUpdate:@"update AccountModel set rests = ? where rests = ?",newRests,oldRests];
    [fmdb executeUpdate:@"update AccountModel set address = ? where address = ?",newAddress,oldAddress];
    [fmdb executeUpdate:@"update AccountModel set time = ? where time = ?",newTime,oldTime];
    [fmdb executeUpdate:@"update AccountModel set type = ? where type = ?",newType,oldType];
    [fmdb executeUpdate:@"update AccountModel set total = ? where total = ?",newTotal,oldTotal];
    [fmdb executeUpdate:@"update AccountModel set consumption = ? where consumption = ?",newConsumption,oldConsumption];
    
    NSLog(@"修改结果:%@",[fmdb lastErrorMessage]);
}

@end
